--[[
	Project....: LUI NextGenWoWUserInterface - Volume Module
	File.......: volume.lua
	Description: Volume Module
	Version....: 1.3
	Rev Date...: 26/11/2010
]]
local LUI = LibStub("AceAddon-3.0"):GetAddon("LUI")
local LSM = LibStub("LibSharedMedia-3.0")
local widgetLists = AceGUIWidgetLSMlists
local LUIHook = LUI:GetModule("LUIHook")
local module = LUI:NewModule("Volume", "AceHook-3.0")
local db

local lastVolume;
local LUIMasterVolumeFrame;

local fontflags = {'OUTLINE', 'THICKOUTLINE', 'MONOCHROME', 'NONE'}

local controls = {
	{
		slider = nil,
		xpos = 0,
		ypos = -40,
		cvar = "Sound_MasterVolume",
		name = "LUIMasterVolumeControlSlider",
		keyword = "Master",
		labelName = "LUIMasterVolumeLabel",
		labelXPos = 0,
		labelYPos = -10,
		labelText = "Master Volume"
	},
	{
		slider = nil,
		xpos = 0,
		ypos = -100,
		cvar = "Sound_SFXVolume",
		name = "LUISoundVolumeControlSlider",
		keyword = "Sound",
		labelName = "LUISoundVolumeLabel",
		labelXPos = 0,
		labelYPos = -70,
		labelText = "Sound Volume"
	},
	{
		slider = nil,
		xpos = 0,
		ypos = -160,
		cvar = "Sound_MusicVolume",
		name = "LUIMusicVolumeControlSlider",
		keyword = "Music",
		labelName = "LUIMusicVolumeLabel",
		labelXPos = 0,
		labelYPos = -130,
		labelText = "Music Volume"
	},
	{
		slider = nil,
		xpos = 0,
		ypos = -220,
		cvar = "Sound_AmbienceVolume",
		name = "LUIAmbienceVolumeControlSlider",
		keyword = "Ambience",
		labelName = "LUIAmbienceVolumeLabel",
		labelXPos = 0,
		labelYPos = -190,
		labelText = "Ambience Volume"
	}
};

local defaults = {
	Volume = {
		Enable = true,
		BackgroundColor = { r = 0.18, g = 0.18, b = 0.18, a = 1 },
		BackgroundTexture = "Blizzard Tooltip",
		BackgroundBorderWidth = 16,
		UseBackgroundColor = false,
		UseBackgroundTexture = true,
		LabelFontColor = { r = 0.99, g = 0.99, b = 0.99, a = 0.99 },
		LabelFont = "AvantGarde_LT_Medium",
		LabelFontSize = 12,
		LabelFontFlags = "THICKOUTLINE",
		BorderTexture = "Stripped_medium",
		IconXOffset = -10,
		IconYOffset = 0,
		IconHeight = 20,
		IconWidth = 20,
	},
}

function module:LoadOptions()
	local options = {
		Volume = {
			name = "Volume",
			type = "group",
			order = 35,
			disabled = function() return not db.Volume.Enable end,
			childGroups = "select",
			args = {
				UseBackgroundTexture = {
					name = "Enable Background Texture",
					desc = "Enables the use of the background texture, while also disabling the use of a background color.",
					disabled = function() return false end,
					type = "toggle",
					get = function() return db.Volume.UseBackgroundTexture; end,
					set = function(self, toggle)
						db.Volume.UseBackgroundTexture = toggle;
						LUIMasterVolume_SetBackground();
					end,
					order = 1,
				},
				BackgroundTexture = {
					name = "Set the background texture.",
					desc = "",
					disabled = function()
						return not db.Volume.UseBackgroundTexture;
					end,
					type = "select",
					order = 2,
					dialogControl = "LSM30_Background",
					values = widgetLists.background,
					get = function() return db.Volume.BackgroundTexture; end,
					set = function(self, texture)
						db.Volume.BackgroundTexture = texture;
						LUIMasterVolume_SetBackground();
					end,
				},
				Spacer1 = {
					name = "   ",
					type = "description",
					order = 3,
				},
				UseBackgroundColor = {
					name = "Enable Background Color",
					desc = "Enables the use of the background color, while also disabling the use of a background texture.",
					disabled = function() return false end,
					type = "toggle",
					get = function() return db.Volume.UseBackgroundColor; end,
					set = function(self, toggle)
						db.Volume.UseBackgroundColor = toggle;
						LUIMasterVolume_SetBackground();
					end,
					order = 4,
				},
				BackgroundColor = {
					name = "Background Color",
					desc = "Determine the background color of the frame",
					disabled = function() 
						return not db.Volume.UseBackgroundColor;
					end,
					type = "color",
					hasAlpha = true,
					get = function() return db.Volume.BackgroundColor.r, db.Volume.BackgroundColor.g, db.Volume.BackgroundColor.b, db.Volume.BackgroundColor.a end,
					set = function(self, r, g, b, a)
						db.Volume.BackgroundColor.r = r
						db.Volume.BackgroundColor.g = g
						db.Volume.BackgroundColor.b = b
						db.Volume.BackgroundColor.a = a
						LUIMasterVolume_SetBackground();
					end,									
					order = 5
				},
				Spacer2 = {
					name = "    ",
					type = "description",
					order = 6,
				},
				BorderWidth = {
					name = "Border Width",
					desc = "",
					disabled = function() return false; end,
					type = "input",
					order = 7,
					get = function() return tostring(db.Volume.BackgroundBorderWidth) end,
					set = function(self, size)
						if (size == nil or size == "") then
							size = "0"
						end
						db.Volume.BackgroundBorderWidth = tonumber(size)
						LUIMasterVolume_SetBackground();
					end
				},
				Spacer3 = {
					name = "     ",
					type = "description",
					order = 8,
				},
				Font = {
					name = "Font",
					desc = "",
					disabled = function() return false; end,
					type = "select",
					dialogControl = "LSM30_Font",
					values = widgetLists.font,
					get = function() return db.Volume.LabelFont; end,
					set = function(self, font)
						db.Volume.LabelFont = font;
						LUIMasterVolume_SetFontColors();
					end,
					order = 9
				},
				FontBorder = {
					name = "Font Outline",
					desc = "",
					disabled = function() return false; end,
					type = "select",
					values = fontflags,
					get = function()
						for k,v in pairs(fontflags) do
							if db.Volume.LabelFontFlags == v then
								return k;
							end
						end
					end,
					set = function(self, flag)
						db.Volume.LabelFontFlags = fontflags[flag];
						LUIMasterVolume_SetFontColors();
					end,
					order = 10
				},
				Spacer4 = {
					name = "      ",
					type = "description",
					order = 11,
				},
				FontColor = {
					name = "Font Color",
					desc = "",
					disabled = function() return false; end,
					type = "color",
					order = 12,
					get = function() return db.Volume.LabelFontColor.r, db.Volume.LabelFontColor.g, db.Volume.LabelFontColor.b, db.Volume.LabelFontColor.a; end,
					set = function(self, r, g, b, a)
						db.Volume.LabelFontColor.r = r;
						db.Volume.LabelFontColor.g = g;
						db.Volume.LabelFontColor.b = b;
						db.Volume.LabelFontColor.a = a;
						LUIMasterVolume_SetFontColors();
					end,
				},
				FontSize = {
					name = "Font Size",
					desc = "",
					disabled = function() return false; end,
					type = "input",
					order = 13,
					get = function() return tostring(db.Volume.LabelFontSize); end,
					set = function(self, size)
						if (size == nil or size == "") then
							size = "12";
						end
						db.Volume.LabelFontSize = tonumber(size);
						LUIMasterVolume_SetFontColors();
					end,
				},
				Spacer5 = {
					name = "        ",
					type = "description",
					order = 14
				},
				BorderTexture = {
					name = "Border Texture",
					order = 15,
					desc = "",
					disabled = function() return false; end,
					type = "select",
					dialogControl = "LSM30_Border",
					values = widgetLists.border,
					get = function() return db.Volume.BorderTexture; end,
					set = function(self, tex)
						db.Volume.BorderTexture = tex;
						LUIMasterVolume_SetBackground();
					end,
				},
				Spacer6 = {
					name = "                 ",
					type = "description",
					order = 16
				},
				IconXOffset = {
					name = "Icon X Offset",
					order = 17,
					desc = "",
					disabled = function() return false; end,
					type = "input",
					get = function() return tostring(db.Volume.IconXOffset); end,
					set = function(self, pos)
						if (pos == nil or pos == "") then
							pos = "0";
						end
						db.Volume.IconXOffset = tonumber(pos);
						LUIMasterVolume_UpdateIcon();
					end
				},
				IconYOffset = {
					name = "Icon Y Offset",
					order = 18,
					desc = "",
					disabled = function() return false; end,
					type = "input",
					get = function() return tostring(db.Volume.IconYOffset); end,
					set = function(self, pos)
						if (pos == nil or pos == "") then
							pos = "0";
						end
						db.Volume.IconYOffset = tonumber(pos);
						LUIMasterVolume_UpdateIcon();
					end
				},
				IconHeight = {
					name = "Icon Height",
					desc = "",
					order = 19,
					disabled = function() return false; end,
					type = "input",
					get = function() return tostring(db.Volume.IconHeight); end,
					set = function(self, height)
						if (height == nil or pos == "") then
							height = "20";
						end
						db.Volume.IconHeight = tonumber(height);
						LUIMasterVolume_UpdateIcon();
					end
				},
				IconWidth = {
					name = "Icon Width",
					desc = "",
					order = 19,
					disabled = function() return false; end,
					type = "input",
					get = function() return tostring(db.Volume.IconWidth); end,
					set = function(self, width)
						if (width == nil or width == "") then
							width = "20"
						end
						db.Volume.IconWidth = tonumber(width);
						LUIMasterVolume_UpdateIcon();
					end
				}
			},
		},
	}

	return options
end

function LUIMasterVolume_SetFontColors()
	local c = db.Volume.LabelFontColor;
	
	for _,i in pairs(controls) do
		local l = getglobal(i.labelName);
		l:SetFont(LSM:Fetch("font", db.Volume.LabelFont), db.Volume.LabelFontSize, db.Volume.LabelFontFlags);
		l:SetTextColor(db.Volume.LabelFontColor.r, db.Volume.LabelFontColor.g, db.Volume.LabelFontColor.b, db.Volume.LabelFontColor.a);
	end
end
function LUIMasterVolume_SetBackground()
	
	local bg = LUI_Media.blank;
	if (db.Volume.UseBackgroundTexture) then
		bg = LSM:Fetch("background", db.Volume.BackgroundTexture);
	elseif (db.Volume.UseBackgroundColor) then
		bg = LSM:Fetch("background", "Solid");
	end
	LUIMasterVolumeFrame:SetBackdrop({
		bgFile = bg,
		edgeFile = LSM:Fetch("border", db.Volume.BorderTexture),
		tile = false,
		edgeSize =  db.Volume.BackgroundBorderWidth,
		tileSize = 0,
		insets = {
			left = 5,
			top = 5,
			right = 5,
			bottom = 5
		}
	});
	if (db.Volume.UseBackgroundColor) then
		local c = db.Volume.BackgroundColor;
		LUIMasterVolumeFrame:SetBackdropColor(c.r, c.g, c.b, c.a);
	end
end
function module:LoadModule()
	local module = {
		Volume = {
			order = LUI:GetModuleCount(),
			type = "execute",
			name = function()
				if db.Volume.Enable then
					return "|cff00ff00Volume Enabled|r"
				else
					return "|cffFF0000Volume Disabled|r"
				end
			end,
			func = function(self, Volume)
				db.Volume.Enable = not db.Volume.Enable
				StaticPopup_Show("RELOAD_UI")
			end,
		},
	}
	
	return module
end

function module:OnInitialize()
	LUI:MergeDefaults(LUI.db.defaults.profile, defaults)
	LUI:RefreshDefaults()
	LUI:Refresh()
	
	self.db = LUI.db.profile
	db = self.db
end

function module:SetValues(field, cvar)
	getglobal("LUI" .. field .. "VolumeControlSliderLow"):SetText("0");
	getglobal("LUI" .. field .. "VolumeControlSliderHigh"):SetText("100");
	getglobal("LUI" .. field .. "VolumeControlSliderText"):SetText(floor(tonumber(GetCVar(cvar)) * 100));
end
function module:CreateSlider(name, cvar, xpos, ypos)
	local frame = CreateFrame("Slider", "LUI"..name.."VolumeControlSlider", LUIMasterVolumeFrame, "OptionsSliderTemplate");
	frame:SetOrientation("HORIZONTAL");
	frame:SetWidth(150);
	frame:SetHeight(20);
	self:SetValues(name, cvar);
	frame:SetPoint("TOP", LUIMasterVolumeFrame, "TOP", xpos, ypos);
	frame:SetScript("OnShow", LUIMasterVolume_OnShow);
	frame:SetScript("OnValueChanged", LUIMasterVolume_OnValueChanged);
	frame:Show();
	return frame;
end
function module:CreateLabel(name, xpos, ypos, txt)
	local label = LUIMasterVolumeFrame:CreateFontString(name, "ARTWORK");
	label:SetFont(LSM:Fetch("font", db.Volume.Font), db.Volume.LabelFontSize, db.Volume.LabelFontFlags);
	label:SetTextColor(db.Volume.LabelFontColor.r, db.Volume.LabelFontColor.g, db.Volume.LabelFontColor.b, db.Volume.LabelFontColor.a);
	label:SetPoint("TOP", LUIMasterVolumeFrame, "TOP", xpos, ypos);
	label:SetJustifyH("CENTER");
	label:SetText(txt);
	label:Show();
end
function LUIMasterVolume_OnShow(self)
	for _, i in pairs(controls) do
		if (i.name == self:GetName()) then
			self:SetMinMaxValues(0, 100);
			self:SetValueStep(1);
			self:SetValue(floor(tonumber(GetCVar(i.cvar)) * 100));
		end
	end
end
function LUIMasterVolume_OnValueChanged(self)
	for _,i in pairs(controls) do 
		if self:GetName() == i.name then
			SetCVar(i.cvar, floor(tonumber(self:GetValue()) / 100));
			getglobal(self:GetName().."Text"):SetText(self:GetValue());
		end
	end
end
function LUIMasterVolume_UpdateIcon()
	local icon = getglobal("Text_speaker");
	icon:SetPoint("RIGHT", "MicroMenu_ButtonLeft_Clicker", "LEFT", db.Volume.IconXOffset, db.Volume.IconYOffset);
	icon:SetWidth(db.Volume.IconWidth);
	icon:SetHeight(db.Volume.IconHeight);

end
function module:CreateVolumeFrame()
	if db.Volume.Enable ~= true then return end;
	
	lastVolume = GetCVar("Sound_MasterVolume");

	local Text_speaker = CreateFrame("Button", "Text_speaker", infos_right);
	LUIMasterVolume_UpdateIcon();
	Text_speaker:SetFrameStrata("TOOLTIP");
	Text_speaker:SetBackdrop({bgFile = "Interface\\Common\\VoiceChat-Speaker", edgeFile = nil, tile = false, edgeSize = 0, insets = { top = 0, right = 0, bottom = 0, left = 0 }});
	Text_speaker:Show();
	
	Text_speaker:RegisterForClicks("AnyUp");
	Text_speaker:SetScript("OnClick", function(self, button)
		if (button == "RightButton") then
			if (tonumber(GetCVar("Sound_MasterVolume")) > 0) then
				lastVolume = GetCVar("Sound_MasterVolume");
				SetCVar("Sound_MasterVolume", 0);
				Text_speaker:SetBackdrop({bgFile = "Interface\\Common\\VoiceChat-Muted", edgeFile = nil, tile = false, edgeSize = 0, insets = {top = 0, right = 0, bottom = 0, left = 0}});
			else
				SetCVar("Sound_MasterVolume", lastVolume);
				Text_speaker:SetBackdrop({bgFile = "Interface\\Common\\VoiceChat-Speaker", edgeFile = nil, tile = false, edgeSize = 0, insets = { top = 0, right = 0, bottom = 0, left = 0 }});
			end
		else
			if (LUIMasterVolumeFrame:IsShown()) then
				LUIMasterVolumeFrame:Hide();
			else
				LUIMasterVolumeFrame:Show();
			end
		end
	end);

	LUIMasterVolumeFrame = CreateFrame("Frame", "LUIMasterVolumeFrame", UIParent);
	LUIMasterVolumeFrame:SetPoint("CENTER", Text_speaker, "CENTER", 0, -130);
	LUIMasterVolumeFrame:SetFrameStrata("HIGH");
	LUIMasterVolumeFrame:SetWidth(180);
	LUIMasterVolumeFrame:SetHeight(255);
	LUIMasterVolume_SetBackground();
	LUIMasterVolumeFrame:Hide();

	for _,i in pairs(controls) do
		i.slider = self:CreateSlider(i.keyword, i.cvar, i.xpos, i.ypos);
		self:CreateLabel(i.labelName, i.labelXPos, i.labelYPos, i.labelText);
	end

end
function module:OnEnable()
	LUI:RegisterOptions(self:LoadOptions())
	LUI:RegisterModule(self:LoadModule())
	self:CreateVolumeFrame();
end
